// pages/detailed-room-page/detailed-room-page.js
Page({ 
    data: {
        //固定头像
        userProfile_common:"https://pic1.imgdb.cn/item/636a34ba16f2c2beb12a52c5.png",
        showLoadingState:false,
        //页面信息 没有实际意义
        pageArray:[0,1,2,3,4,5,6,7,8,9,10,11],
        //图片
        p1:"https://pic.imgdb.cn/item/62ea3b7d16f2c2beb1307ec3.png",
        p2:"https://pic.imgdb.cn/item/62ea3b7d16f2c2beb1307ec6.png",
        p3:"https://pic.imgdb.cn/item/62ea3b7d16f2c2beb1307ed6.png",
        p4:"https://pic.imgdb.cn/item/62ea3b7d16f2c2beb1307eee.png", 
        p5:"https://pic.imgdb.cn/item/62ea3b7d16f2c2beb1307efb.png",
        p6:"https://pic.imgdb.cn/item/62ea3ba016f2c2beb130ad11.png",
        p7:"https://pic.imgdb.cn/item/62d68d1ef54cd3f93723f49d.png",//空位置图标
        p8:"https://pic.imgdb.cn/item/62f37ab116f2c2beb17f5abc.png",//禁止位置图标
        //数据转换
        sportsArray:['所有项目','足球','篮球','排球','羽毛球','乒乓球','棒球','网球'],
        //房间具体信息
        room:{
            room_id:4,
            room_sports:2,
            room_location:"大学生活动中心",
            room_playground:"场地一",
            room_date:{
                year:2022,
                month:7,
                day:3,
                starttime:20,
                endtime:22,
            },
            room_price:10,
            room_all_person:8,
            room_now_person:3,
            room_lack_person:5,
            room_state:1,
            room_host_id:1243,
        },
        persons:[
            //这是一个加入此房间的所有用户的信息数组，按照先已确认，再未确认进行排序
            {
                user_name:'sdfasfda',
                user_profile:'https://pic.imgdb.cn/item/62f37e9716f2c2beb18da207.png',//用户头像的url
                pk_value:5,
                user_state:0,//用户的状态，有未确认0和已确认1
            },
            {
                user_name:'asdfjk',
                user_profile:'https://pic.imgdb.cn/item/62f37e9716f2c2beb18da207.png',//用户头像的url
                pk_value:5,
                user_state:0,//用户的状态，有未确认0和已确认1
            },
            {
                user_name:'sdfasfda',
                user_profile:'https://pic.imgdb.cn/item/62f37e9716f2c2beb18da207.png',//用户头像的url
                pk_value:5,
                user_state:0,//用户的状态，有未确认0和已确认1
            },
        ],

        //在线联系
        setIntervalId:null,
        hideChat:true,
        inputMessage:"",
        messageArray:[
            {
                user_name:123,
                content:"明天1231231231111111111111111111111111111111111111111111",
            },
            {
                user_name:456,
                content:"就今天吧123就今天吧123就今天吧123就今天吧123就今天吧123",
            },
            {
                user_name:123,
                content:"明天约吧",
            },
            {
                user_name:456,
                content:"就今天吧",
            },
            {
                user_name:123,
                content:"明天约吧",
            },
            {
                user_name:456,
                content:"就今天吧",
            },
            {
                user_name:123,
                content:"明天约吧",
            },
            {
                user_name:456,
                content:"就今天吧",
            },
            {
                user_name:123,
                content:"今天有事",  
            }
        ],
        messageNum:0,
    },
    onLoad(options) {
        //向服务器获取房间信息
        var that = this;
        wx.request({
            url: getApp().globalData.serverUrl,
            method:"GET",
            timeout:10000,
            data:{
              //各种数据的传送
              operation:"DETAILED_ROOM",
              roomId:options.roomid,
            },
            success(res){
              if(res.data == false){
                  wx.showModal({
                      showCancel:false,
                      title:"请求失败,请重试",
                  });
              }
              else{
                  //成功获取到信息后的操作（赋值——room and persons）
                  console.log(res)
                that.setData({
                    'persons':res.data.persons,
                    "room":res.data.room,
                });
                that.onLoadChat();
                //设置定时开始获取聊天信息
                // var x = setInterval(that.onLoadChat,10000);
                // console.log(x);
                // that.setData({
                //     "setIntervalId":x,
                // });
              }
            },
            fail(res){
              wx.showModal({
                  showCancel:false,
                  title:"请求超时,请重试",
              });
            },
          });  
        //room结构体和persons人员信息
    },
    onUnload(options){
        clearInterval(this.data.setIntervalId);
    },
    onTapUserProfile:function(e){
        var that = this;
        var x = e.currentTarget.dataset.index;
        console.log(e);
        wx.showModal({
          title:that.data.persons[x].user_name,
          content:'PK值：'+that.data.persons[x].pk_value,
          showCancel:false,
        })
    },
    onEnterRoom:function(e){//加入房间
        var that = this;
        //加入房间，刷新
        if(that.onJudge()){
            //用户已经加入房间
            wx.showModal({
              showCancel:false,
              title:"您已在房间内，请勿重复加入",
            })
        }
        else if(this.data.room.room_all_person - this.data.room.room_now_person == 0){
            wx.showModal({
              showCancel:false,
              title:"抱歉，人数已满",
            })
        }
        else{//用户未加入房间
            //向服务器申请
        that.onShowLoading();
        wx.request({
            url: getApp().globalData.serverUrl,
            method:"GET",
            timeout:10000,
            data:{
              //各种数据的传送
              operation:"JOIN_ROOM",
              studentId:getApp().globalData.userInfo.studentId,
              roomId:that.data.room.room_id,
              sportsNum:that.data.room.room_sports,
            },
            success(res){
              if(res.data == false){
                  that.onHideLoading()
                  wx.showModal({
                      showCancel:false,
                      title:"请求失败,请重试",
                  });
              }
              else{
                  //成功获取到信息后的操作
                  that.onHideLoading();
                  that.onShowLoading();
                  wx.request({
                    url: getApp().globalData.serverUrl,
                    method:"GET",
                    timeout:10000,
                    data:{
                      //各种数据的传送
                      operation:"DETAILED_ROOM",
                      roomId:that.data.room.room_id,
                    },
                    success(res){
                      if(res.data == false){
                          that.onHideLoading();
                          wx.showModal({
                              showCancel:false,
                              title:"请求失败,请重试",
                          });
                      }
                      else{
                          //成功获取到信息后的操作（赋值——room and persons）
                        that.setData({
                            'persons':res.data.persons,
                            "room":res.data.room,
                        });
                        that.onHideLoading();
                      }
                    },
                    fail(res){
                        that.onHideLoading();
                      wx.showModal({
                          showCancel:false,
                          title:"请求超时,请重试",
                      });
                    },
                  });  
              }
            },
            fail(res){
                that.onHideLoading();
              wx.showModal({
                  showCancel:false,
                  title:"请求超时,请重试",
              });
            },
          });
        }
    },
    onExitRoom:function(e){//退出房间按钮
        var that = this;
        //是房主 解散
        if(getApp().globalData.userInfo.studentId == that.data.room.room_host_Id){
            var that = this;
            wx.showModal({
              title:"您的退出会解散房间",
              success(res){
                if(res.confirm){
                    that.onShowLoading();
                    wx.request({
                      url: getApp().globalData.serverUrl,
                      method:"GET",
                      timeout:10000,
                      data:{
                        operation:"DELETE_ROOM",
                        studentId:getApp().globalData.userInfo.studentId,
                        roomId:that.data.room.room_id,
                      },
                      success(res){
                        if(res.data == false){
                            that.onHideLoading();
                            wx.showModal({
                                showCancel:false,
                                title:"请求失败,请重试",
                            });
                        }
                        else{
                            //成功获取到信息后的操作
                            that.onHideLoading();
                            wx.showModal({
                              showCancel:false,
                              title:"房间已解散",
                            })
                            wx.navigateBack({
                                delta:2,
                            });
                        }
                      },
                      fail(res){
                          that.onHideLoading();
                          console.log(res);
                        wx.showModal({
                            showCancel:false,
                            title:"请求超时,请重试",
                        });
                      },
                    });
                }
              },
            })
        }
        else if(that.onJudge()){//已经加入 退出服务器 刷新

            var that = this;
            that.onShowLoading();
            wx.request({
              url: getApp().globalData.serverUrl,
              method:"GET",
              timeout:10000,
              data:{
                operation:"QUIT_ROOM",
                studentId:getApp().globalData.userInfo.studentId,
                roomId:that.data.room.room_id,
              },
              success(res){
                if(res.data == false){
                    that.onHideLoading();
                    wx.showModal({
                        showCancel:false,
                        title:"请求失败,请重试",
                    });
                }
                else{
                    that.onHideLoading();
                    that.onRefreshData();
                }
              },
              fail(res){
                  that.onHideLoading();
                wx.showModal({
                    showCancel:false,
                    title:"请求超时,请重试",
                });
              },
            });
        }
        else{        //未加入 退出页面
            wx.navigateBack({
                delta:2,
            });
        }
    },
    onSendChat:function(e){//发送消息
        //发送信息
        var that = this;
        that.onShowLoading();
        wx.request({
          url: getApp().globalData.serverUrl,
          method:"GET",
          timeout:10000,
          data:{
            //各种数据的传送
            operation:"USER_SEND",
            roomId:that.data.room.room_id,
            userId:getApp().globalData.userInfo.studentId,
            content:that.data.inputMessage,
          },
          success(res){
            if(res.data == false){
                that.onHideLoading();
                wx.showModal({
                    showCancel:false,
                    title:"请求失败,请重试",
                });
            }
            else{
                //成功获取到信息后的操作
                that.onHideLoading();
                that.onHideChat();
            }
          },
          fail(res){
              that.onHideLoading();
            wx.showModal({
                showCancel:false,
                title:"请求超时,请重试",
            });
          },
        });
    },
    onLoadChat:function(e){//加载历史记录
        var that = this;
        wx.request({
          url: getApp().globalData.serverUrl,
          method:"GET",
          timeout:10000,
          data:{
            //各种数据的传送
            operation:"USER_RECEIVE",
            roomId:that.data.room.room_id,
          },
          success(res){
            if(res.data == false){
                console.log(res)
                wx.showModal({
                    showCancel:false,
                    title:"请求失败,请重试",
                });
            }
            else{
                //成功获取到信息后的操作
                console.log(res);
                that.setData({
                    "messageArray":res.data.ml,
                });
                that.setData({
                    "messageNum":that.data.messageArray.length,
                });
                
            };
          },
          fail(res){
              console.log(res)
            wx.showModal({
                showCancel:false,
                title:"请求超时,请重试",
            });
          },
        });
    },
    onShowChat:function(e){//显示对话框
        this.setData({
            'hideChat':false,
            "messageNum":this.data.messageArray.length,
        });
        this.onLoadChat();
    },
    onHideChat:function(e){//隐藏对话框
        this.setData({
            'hideChat':true,
        })
    },
    onTapChat:function(e){//点击在线联系按钮
        if(this.data.hideChat){
            this.onShowChat();
        }
        else
        {
            this.onHideChat();
        }
    },
    onInput:function(e){//失去焦点
        this.setData({
            'inputMessage':e.detail.value,
        });
    },
    onJudge:function(e){//判断当前用户是否已经加入房间
        var id = getApp().globalData.userInfo.userName;
        var personsArray = this.data.persons;
        for(var i = 0;i < personsArray.length;i ++){
            if(id == personsArray[i].user_name){
                return true;
            }
        }
        return false;
    },
    onRefreshData:function(e){//刷新房间数据
        var that = this;
        wx.request({
            url: getApp().globalData.serverUrl,
            method:"GET",
            timeout:10000,
            data:{
              //各种数据的传送
              operation:"DETAILED_ROOM",
              roomId:that.data.room.room_id,
            },
            success(res){
              if(res.data == false){
                  wx.showModal({
                      showCancel:false,
                      title:"请求失败,请重试",
                  });
              }
              else{
                  //成功获取到信息后的操作（赋值——room and persons）
                  console.log("已刷新所有数据"+ "\n" +res)
                that.setData({
                    'persons':res.data.persons,
                    "room":res.data.room,
                });
              }
            },
            fail(res){
              wx.showModal({
                  showCancel:false,
                  title:"请求超时,请重试",
              });
            },
          });  
    },
    onShowLoading:function(e){
        var that = this;
        wx.showLoading({
          title: '加载中',
          mask:true,
          success(res){
              that.setData({
                  "showLoadingState":true,
              })
          }
        })
        setTimeout(that.onHideLoading,10200)
    },
    onHideLoading:function(e){
        var that = this;
        if(that.data.showLoadingState == true){
            wx.hideLoading();
            that.setData({ 
                "showLoadingState":false,
            })
        }
    },
})